package cn.gson.crm.controller;

import cn.gson.crm.model.dao.OrderDao;
import cn.gson.crm.model.dao.OrderDetailsDao;
import cn.gson.crm.model.mapper.OrderMapper;
import cn.gson.crm.model.pojos.*;
import cn.gson.crm.model.service.OrderSerivice;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/OrderContract")
public class OrderContract {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderDao orderDao;
    @Autowired
    OrderDetailsDao orderDetailsDao;
    @Autowired
    OrderSerivice orderSerivice;
    //查询负责人下所有合同
    @RequestMapping("/ContractMethod")
    @ResponseBody
    public Map<String,Object> selectorder(Integer cid,String dept,Integer page,Integer size,String jinnian,String qunian,String qiannian){
        List<Order> orders2=orderMapper.selectorder(cid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Order> orders=orderMapper.selectorder(cid);
        //订单号
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        List<Employee> employeeList=orderMapper.selectLB();
        List<Employee> employeeList2=orderMapper.selectsp(dept);
        List<Contract> contractList=orderMapper.selectcontract(cid);
        Map<String,Object> map = new HashMap<>();
        //统计图时间查询数据
        List<Order> contractList4=orderMapper.selectnian(cid,jinnian);
        List<Order> contractList5=orderMapper.selectnian(cid,qunian);
        List<Order> contractList6=orderMapper.selectnian(cid,qiannian);
        map.put("total",obj.getTotal());
        map.put("rows",orders);
        map.put("order","DD"+newDate+result);
        map.put("jinli",employeeList2);
        map.put("lb",employeeList);
        map.put("contrat",contractList);
        map.put("tong",orders2);
        map.put("jinnian",contractList4);
        map.put("qunian",contractList5);
        map.put("qiannian",contractList6);
        return map;
    };
    //根据合同id查询此合同的报价
    @RequestMapping("/ContractMethod2")
    @ResponseBody
    public List<Sales> ContractMethod(Integer cid){
        System.out.println("--------"+cid);
        return  orderSerivice.selectbaojia(cid);
    };
    //新增订单
    @RequestMapping("/ContractMethod3")
    @ResponseBody
    public void ContractMethod1(HttpSession session,@RequestBody Order order){
       orderSerivice.saveorder(session,order);
    };
    //条件查询订单状态
    @RequestMapping("/ContractMethod4")
    @ResponseBody
    public Map<String,Object> selectstate( HttpSession session, Integer cid, String state,Integer page,Integer size){
        return orderSerivice.selectstate(session,cid,state,page,size);
    };
    //根据合同编号和订单号查询订单
    @RequestMapping("/ContractMethod5")
    @ResponseBody
    public Map<String,Object> selectwhere( Integer cid, String ocnumber,Integer page,Integer size){
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Order> orders=orderMapper.selectwhere(cid,ocnumber);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",orders);
        return map;
    };
    //高级查询
    @RequestMapping("/ContractMethod6")
    @ResponseBody
    public Map<String,Object> selectgaoji(HttpSession session, Integer cid, String onumber, String cnumber,  String condition6,String ecstate,Integer page, Integer size){
        return orderSerivice.selectgaoji(session,cid,onumber,cnumber,condition6,ecstate,page,size);
    };
    //根据id查询订单详情
    @RequestMapping("/ContractMethod8")
    @ResponseBody
    public List<OrderDetails> selectOder(Integer oid){
      return orderSerivice.selectoid(oid);
    }
    //主页查询
    @RequestMapping("/selectOver")
    @ResponseBody
    public Map<String ,Object> selectOver(String orderTime){
        Map<String,Object> map=null;
        if(orderTime.equals("2020")){
            map=orderSerivice.selectOver(orderTime);
        }
        if(orderTime.equals("2019")){
            map=orderSerivice.selectOver2(orderTime);
        }
        return map;
    }
    @RequestMapping("/selectMoney")
    @ResponseBody
    public Map<String,Object> selectMoney(String type){
        if(type.equals("合同金额")){
            return orderSerivice.selectMoney();
        }
        else {
            return orderSerivice.selectMoney2();
        }

    }
}
